﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Text;
using System.Windows.Markup;

namespace WQDSkin.Forms.Controls
{
    public class SystemKeyConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == null)
            {
                throw new ArgumentNullException("sourceType");
            }

            return base.CanConvertFrom(context, sourceType);
        }


        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            // Validate Input Arguments
            if (destinationType == null)
            {
                throw new ArgumentNullException("destinationType");
            }
            else if (destinationType == typeof(MarkupExtension) )
            {
                return true;
            }

            return base.CanConvertTo(context, destinationType);

        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            return base.ConvertFrom(context, culture, value);
        }


        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
 
            if (destinationType == null)
            {
                throw new ArgumentNullException("destinationType");
            }


            if (destinationType == typeof(MarkupExtension) && CanConvertTo(context, destinationType))
            {
                WQDSystemResourceKeyID keyId; 

                if (value is WQDSystemResourceKey)
                {
                    //keyId = (value as WQDSystemResourceKey).InternalKey;
                }
                //else if (value is SystemThemeKey)
                //{
                //    keyId = (value as SystemThemeKey).InternalKey;
                //}
                //else
                //{
                //    throw new ArgumentException(SR.Get(SRID.MustBeOfType, "value", "SystemResourceKey or SystemThemeKey"));
                //}

                // System resource keys can be converted into a MarkupExtension (StaticExtension)

                //Type keyType = SystemKeyConverter.GetSystemClassType(keyId);

                // Get the value serialization context
                //IValueSerializerContext valueSerializerContext = context as IValueSerializerContext;
                //if (valueSerializerContext != null)
                //{
                //    // And from that get a System.Type serializer
                //    ValueSerializer typeSerializer = valueSerializerContext.GetValueSerializerFor(typeof(Type));
                //    if (typeSerializer != null)
                //    {
                //        // And use that to create the string-ized class name
                //        string systemClassName = typeSerializer.ConvertToString(keyType, valueSerializerContext);

                //        // And finally create the StaticExtension.
                //        return new StaticExtension(systemClassName + "." + GetSystemKeyName(keyId));
                //    }
                //}
            }

            return base.CanConvertTo(context, destinationType);
        }

    }
}
